import * as _ from "lodash"
import httpClient from "../httpclient"

export class User {
  public id: string
  public username: string
  public nickname: string
  public mobile: string
  public avatar: string
  private country_code: string

  public get countryCode() {
    return this.country_code
  }

  public set countryCode(countryCode) {
    this.country_code = countryCode
  }
}

export enum UserRegistrationMode {
  Mail = "mail", Mobile = "mobile"
}

export class UserRegistration {
  public static readonly CAPTCHA_TOKEN_PARAMETER_NAME = "captcha_token"
  public static readonly CAPTCHA_CODE_PARAMETER_NAME = "captcha_code"

  public username: string
  public nickname: string
  public mobile: string
  public mail: string
  public mode: UserRegistrationMode
  public parameters: { [key: string]: string } = {}

  public setParameter(name: string, value: string) {
    this.parameters[name] = value
  }
}

export class UserServiceV1 {

  public static createUser(registration: UserRegistration): Promise<User> {
    return httpClient.post(`/v1/users`, registration)
      .then(({ data }) => _.assign(new User(), data))
  }

  public static getUser(userId: string): Promise<User> {
    return httpClient.get(`/v1/users/${userId}`)
      .then(({ data }) => _.assign(new User(), data))
  }

  public static getCurrentUser(): Promise<User> {
    return httpClient.get(`/v1/users/current`)
      .then(({ data }) => _.assign(new User(), data))
  }
}
